package example;

import org.lwjgl.input.Keyboard;

import core.BlendMode;
import core.Colour;
import core.Game;
import core.Texture2D;

public class RenderTargetExample extends Game {

   Texture2D texture;
   Texture2D texture2;
   Texture2D buffer;
   float posx;
   float posy;
   float alpha = 1.0f;
   public RenderTargetExample(boolean vsync) {
      super(vsync);
   }
   public RenderTargetExample(String name, int width, int height, boolean resizable) {
      super(name,width,height,resizable);
      setFPS(60);
   }

   @Override
   public void init() {
      texture = new Texture2D("resources/gfx/bacon.png");
      texture2 = new Texture2D("resources/gfx/link1.png");
      buffer = new Texture2D();
   }
 
   @Override
   public void update(long deltaTime) {
      super.update(deltaTime);
      alpha -= 0.001f;
      
      double deltaSeconds = deltaTime / 1.0e9;
      if(Keyboard.isKeyDown(Keyboard.KEY_W)){
         posy -= (10 * 60) * deltaSeconds; 
      } 
      if(Keyboard.isKeyDown(Keyboard.KEY_S)){
         posy += (10 * 60) * deltaSeconds;
      };
      if(Keyboard.isKeyDown(Keyboard.KEY_A)){
         posx -= (10 * 60) * deltaSeconds;
      }
      if(Keyboard.isKeyDown(Keyboard.KEY_D)){
         posx += (10 * 60) * deltaSeconds;
      }
   }
   
   
   @Override
   public void render() {
      super.render();
      //Change the render target to the texture
      spriteBatch.changeRenderTarget(buffer);
      spriteBatch.begin();
      spriteBatch.draw(texture, (int)posx , (int)posy, new Colour(1f,1f,1f,1f));
      spriteBatch.draw(texture2, 0 ,200, new Colour(1,1,1,alpha));
      spriteBatch.end();
      
      //Render to the screen
      spriteBatch.defaultRenderTarget();
      spriteBatch.begin();
      spriteBatch.draw(buffer, 0, 0, 600, 200, Colour.White);
      spriteBatch.draw(buffer, 200, 200, 100, 100, Colour.White);
      for(int i = 0; i < 10; i++){
         spriteBatch.draw(buffer, i * 100, 300, 100, 100, Colour.White);
      }
      spriteBatch.draw(buffer, 0, 0, new Colour(1, 1, 1, 0.5f));
      
      spriteBatch.end();
   }//End method render
   
   public static void main(String[] args){
      new RenderTargetExample("", 800, 600, true).run();
   }
}
